package com.zz.retryall.aop;

import com.zz.retryall.aop.annotation.Retryable;
import com.zz.retryall.exception.HelloRetryException;
import com.zz.retryall.service.IHelloService;
import com.zz.retryall.service.NameService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalTime;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
@Service
public class HelloForAopService implements IHelloService{

    public static AtomicInteger helloTimes = new AtomicInteger();

    @Autowired
    NameService nameService;

    @Retryable(retryTimes = 4, retryInterval = 2)
    public String hello()  {

        long times = helloTimes.incrementAndGet();
        log.info("hello times:{}", times);
        if (times % 4 != 0){
            log.warn("发生异常，time：{}", LocalTime.now() );
            throw new HelloRetryException("发生Hello异常");
        }
        return "hello " + nameService.getName();

    }
}
